//
//  GCMYIChaobiaoViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/10/13.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMYIChaobiaoViewController.h"
#import "GCMChaoBiaoTableViewCell.h"
#import "GCMChaobiaoModel.h"

@interface GCMYIChaobiaoViewController ()<UITableViewDelegate,UITableViewDataSource>{

    UITableView *_tableV;
    UIButton *_subButton;
}

@end

@implementation GCMYIChaobiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self createTableView];
    // Do any additional setup after loading the view from its nib.
}

-(void)downLoadData{


    
}
-(void)createUI{

    _subButton = [[UIButton alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT - 55, SCREEN_WIDTH - 40, 50)];
    _subButton.backgroundColor = [UIColor colorWithRed:0.25 green:0.44 blue:0.63 alpha:1];
    _subButton.layer.cornerRadius = 4;
    [_subButton setTitle:@"保存" forState:UIControlStateNormal];
    [_subButton addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_subButton];
}
-(void)createTableView{
    
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT - 60 - 64)];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMChaoBiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMChaoBiaoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    NSLog(@"66666%@",self.dataSource);
    GCMChaobiaoModel *chaobiaoModel = [[GCMChaobiaoModel alloc]init];
    chaobiaoModel = self.dataSource[0];
    [cell setTextChangedBlock:^(NSString *number) {
        chaobiaoModel.byds = number;
        chaobiaoModel.isSave = @"1";
    }];
    cell.erjiPage = @"2";
    [cell setChabiaoModel:chaobiaoModel];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return SCREEN_HEIGHT/3;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
}



//一键保存数据
-(void)saveData{
    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"是否立即提交数据" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertControl addAction:[UIAlertAction actionWithTitle:@"马上提交" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // 点击确定按钮的时候, 会调用这个block
        NSLog(@"aaaa");
        NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
        
        GCMChaobiaoModel *chaobiaoModel = self.dataSource[0];
        
        NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
        NSString *zb = [NSString stringWithFormat:@"%f",[chaobiaoModel.byds floatValue] - [chaobiaoModel.syds floatValue]];
        NSString *money = [NSString stringWithFormat:@"%f",[zb floatValue]*[chaobiaoModel.dj floatValue]];
        //        NSLog(@"+nihao +1+  %@",chaobiaoModel.dj );
        [d setObject:chaobiaoModel.idd forKey:@"id"];
        [d setObject:money forKey:@"money"];
//        处理四舍五入
        double sss = round([chaobiaoModel.byds doubleValue]*10)/10;
        NSString *ss = [NSString stringWithFormat:@"%fd",sss];
        NSArray * arrar1 = [ss  componentsSeparatedByString:@"."];
        NSRange range2 =  NSMakeRange(0, 1);
        NSString * subStr3 = [arrar1[1] substringWithRange:range2];
        NSString * iii = [NSString stringWithFormat:@"%@.%@",arrar1[0],subStr3];
        [d setObject:iii forKey:@"byds"];
        
        [d setObject:zb forKey:@"zbl"];
        [d setObject:chaobiaoModel.valid forKey:@"valid"];
        NSString *str  = [[NSString alloc]init];
        str = [GCMCBLRRequst stringWithJsonDictionary:d];
        
        parameters[@"dataJson"] = [GCMCBLRRequst stringWithJsonDictionary:d];
        
//        NSLog(@"+nihao +1+  %@",parameters);
        
        [GCMCBLRRequst postCBXIUGAIWithData:parameters Success:^(id obj, NSError *err) {
            NSMutableArray * a  = [[NSMutableArray alloc]init];
            a = obj;
//            NSLog(@"%@",a);
            BOOL i = [a valueForKey:@"result"];
            if (1 == i){
                [self.navigationController popViewControllerAnimated:YES];
                [ProgressHUD showSuccess:[a valueForKey:@"data"]];
            }else{
                [ProgressHUD showError:[a valueForKey:@"error"]];
            }
        }];
        
    }]];
    
    [alertControl addAction:[UIAlertAction actionWithTitle:@"暂不处理" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alertControl animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
